package cn.com.winning.ecare.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.com.winning.ecare.common.MyApplication;
import cn.com.winning.ecare.utils.HTTPGetTool;
import cn.com.winning.ecare.utils.URLUtils;
import cn.com.winning.ecare.widgets.CustomProgressDialog;

/**
 * 挂号科室
 * @author Administrator
 *
 */
public class GhTwo extends Activity
{
	private ImageButton ks_preview = null;
	private ListView lv = null;

	private CustomProgressDialog proDialog;// 加载框
	private GhTwo oThis = this;

	private Intent mIntent = null;
	private Map<String, Object> map_ks; // 科室map
	List<Map<String, Object>> list_ks = new ArrayList<Map<String, Object>>();// 科室list
	MyApplication myApp;

	private int preID = -1;
	private MyAdapter myAdapter;
	private ImageView menu_img;
	private ImageButton mainselected;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.setContentView(R.layout.gh_two);
		myApp = (MyApplication) getApplication();
		myApp.addActivity(this);
		// 确定view
		findView();

		// 初始化界面数据
		GetData();

		// 点击事件
		setOnClick();
	}

	private void GetData()
	{
		proDialog = CustomProgressDialog.createDialog(oThis);
		proDialog.setMessage("数据加载中...");
		proDialog.show();
		proDialog.setCanceledOnTouchOutside(false);
		// 这里单开一个线程和后台数据交互
		new Thread(new LoadData()).start();

	}

	class LoadData implements Runnable
	{

		public void run()
		{
			// TODO Auto-generated method stub
			Looper.prepare();
			iniData();
			Looper.loop();
		}
	}

	private void iniData()
	{
		try
		{
			Message msg = myHandler.obtainMessage();
			List<NameValuePair> ls = new ArrayList<NameValuePair>();
			ls.add(new BasicNameValuePair("yydm", myApp.getYydm()));
			JSONObject json = HTTPGetTool.getTool().post(URLUtils.HOST + URLUtils.GHGETKSLIST, ls);
			JSONArray jsonArr;
			List<Map<String, Object>> tempmain = new ArrayList<Map<String, Object>>();
			if (json != null)
			{
				// 获取科室信息
				jsonArr = new JSONArray(json.getString("ksinfo"));
				JSONObject js = null;
				for (int i = 0; i < jsonArr.length(); i++)
				{
					map_ks = new HashMap<String, Object>();
					js = jsonArr.getJSONObject(i);
					// previewId = js.getString("PId");
					map_ks.put("ksdm", js.getString("ksdm"));
					map_ks.put("ksmc", js.getString("ksmc"));
					tempmain.add(map_ks);
				}
				msg.what = 0;
				msg.obj = tempmain;
			}
			else
			{
				msg.what = -1;
				msg.obj = "网络连接错误！";
			}

			myHandler.sendMessage(msg);
		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			Message msg = myHandler.obtainMessage();
			msg.what = -1;
			msg.obj = e.toString();
			myHandler.sendMessage(msg);
		}
	}

	/**
	 * 主线程状态更新
	 */
	Handler myHandler = new Handler()
	{
		public void handleMessage(Message msg)
		{
			switch (msg.what)
			{
				case -1:
					if (proDialog != null && proDialog.isShowing())
					{
						proDialog.dismiss();
					}
					Toast.makeText(oThis, (String) msg.obj, Toast.LENGTH_SHORT).show();
					break;
				case 0:
					if (proDialog != null && proDialog.isShowing())
					{
						proDialog.dismiss();
					}
					if(msg.obj!=null){
						list_ks = (List<Map<String, Object>>) msg.obj;
					}
					
					myAdapter = new MyAdapter(oThis);
					myAdapter.setList(list_ks);
					lv.setAdapter(myAdapter);
					myAdapter.notifyDataSetChanged();
					break;
				default:
					break;
			}
		};
	};

	private void setData()
	{
		myAdapter = new MyAdapter(oThis);
		myAdapter.setList(list_ks);
		lv.setAdapter(myAdapter);
	}

	class MyAdapter extends BaseAdapter
	{
		private List<Map<String, Object>> list;
		private LayoutInflater mLayoutInflater;

		MyAdapter(Context context)
		{
			mLayoutInflater = LayoutInflater.from(context);
		}

		public List<Map<String, Object>> getList()
		{
			return list;
		}

		public void setList(List<Map<String, Object>> list)
		{
			this.list = list;
		}

		@Override
		public int getCount()
		{
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position)
		{
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position)
		{
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			// TODO Auto-generated method stub
			ViewHolder holder = null;
			if (convertView == null)
			{
				convertView = mLayoutInflater.inflate(R.layout.gh_item_two, null);
				holder = new ViewHolder();
				holder.tv_id = (TextView) convertView.findViewById(R.id.ks_id);
				holder.tv_mc = (TextView) convertView.findViewById(R.id.ks_mc);
				convertView.setTag(holder);
			}
			else
			{
				holder = (ViewHolder) convertView.getTag();
			}
			holder.tv_id.setText((CharSequence) list.get(position).get("ksdm"));
			holder.tv_mc.setText((CharSequence) list.get(position).get("ksmc"));
			if (position % 2 == 0)
			{// 偶数
				if (position == preID)
					convertView.setBackgroundColor(getResources().getColor(R.color.listhead_color));
				else
					convertView.setBackgroundColor(getResources().getColor(R.color.backgroundMYD));
			}
			else
			{// 奇数
				if (position == preID)// 选中
					convertView.setBackgroundColor(getResources().getColor(R.color.listhead_color));
				else
					convertView.setBackgroundColor(getResources().getColor(R.color.white_bg));
			}
			return convertView;
		}

	}

	public class ViewHolder
	{
		private TextView tv_id;
		private TextView tv_mc;
	}

	private void setOnClick()
	{
		// 设置preview监听
		ks_preview.setOnClickListener(new View.OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				// TODO Auto-generated method stub

				Intent intent = getIntent();
				GhTwo.this.setResult(0, intent);// 跳转回原来的activity
				GhTwo.this.finish();// 一定要结束当前activity

			}
		});

		// 设置下一级菜单
		lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
		{

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
			{
				// TODO Auto-generated method stub
				myAdapter.notifyDataSetChanged();
				TextView ks_mc = (TextView) arg1.findViewById(R.id.ks_mc);
				TextView ks_id = (TextView) arg1.findViewById(R.id.ks_id);

				Intent intent = getIntent();
				intent.putExtra("ks_mc", ks_mc.getText().toString());
				intent.putExtra("ks_id", ks_id.getText().toString());
				preID = arg2;
				GhTwo.this.setResult(0, intent);// 跳转回原来的activity
				GhTwo.this.finish();// 一定要结束当前activity
			}
		});
		menu_img.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View view)
			{
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setClass(oThis, GhSeven.class);
				startActivity(intent);
			}
		});
		mainselected.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View view)
			{
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setClass(oThis, Mainmean.class);
				startActivity(intent);
				oThis.finish();
			}
		});
	}

	private void findView()
	{
		mIntent = getIntent();
		lv = (ListView) this.findViewById(R.id.ks_list);
		ks_preview = (ImageButton) this.findViewById(R.id.ks_preview);
		menu_img = (ImageView) this.findViewById(R.id.menu_yy_img);
		mainselected = (ImageButton) this.findViewById(R.id.mainselected);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
		{
			Intent intent = getIntent();
			GhTwo.this.setResult(0, intent);// 跳转回原来的activity
			GhTwo.this.finish();// 一定要结束当前activity
			return false;
		}
		return false;
	}

}
